//
//  WSRechargeController.m
//  CharonTVProject
//
//  Created by karong on 2023/8/10.
//

#import "WSRechargeController.h"
#import "WSRechargeCell.h"
#import "WSPurchaseRequest.h"
#import "AppDelegate.h"
#import "WSPurchaseManager.h"
#import "WSPurchaseFooterView.h"
#import "WSGoods.h"
#import "CTMediator+WSUserCenterAction.h"

@interface WSRechargeController () <WSPurchaseManagerDelegate, WSPurchaseFooterViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSArray * dataArray;
@property (weak, nonatomic) IBOutlet UIImageView *userImageView;
@property (weak, nonatomic) IBOutlet UILabel *coinsLabel;
@property (weak, nonatomic) IBOutlet UILabel *userNameLabel;
@end

@implementation WSRechargeController
@synthesize dataArray = _dataArray;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addObserver];
    [self layoutMainView];
    [self loadGoodsListRequest];
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(layoutMainView) name:WSCoinsChangeNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(layoutMainView) name:WSRefreshUserInfoNotification object:nil];
}

- (void)layoutMainView {
    self.userNameLabel.text = appUserCenter.user_name;
    self.coinsLabel.text = WSSF(@"%@：%ld剧豆", APPLocalized(@"剧豆餘額"),(long)appUserCenter.coins);
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:appUserCenter.avatar] placeholderImage:WSPlaceholderImage];
}

- (void)loadGoodsListRequest {
    WeakSelf
    [WSToast loading];
    [WSPurchaseRequest loadcharonTV_GoodsListWithGoods_type:1 success:^(NSArray *array) {
        weakSelf.dataArray = array;
        [WSPurchaseManager shared].delegate = weakSelf;
        [WSPurchaseManager shared].products = array;
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (void)payGoods:(WSGoods *)goods {
    if (goods.SKProduct) {
        if (appUserCenter.isLogin) {
            [WSPurchaseManager shared].delegate = self;
            [[WSPurchaseManager shared] createOrderWithProduct:goods];
        }
        else {
            [((AppDelegate*)([UIApplication sharedApplication].delegate)) initMainPage];
        }
    }
}

- (IBAction)recordAction:(id)sender {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_CoinRecords];
    [self.navigationController pushViewController:target animated:YES];
}

#pragma mark - WSPurchaseManagerDelegate
- (void)didAppleReceiveResponse {
    [WSToast dismiss];
    [self.listView reloadData];
}

- (void)purchaseManager:(WSPurchaseManager *)manager updateStatus:(WSPurchaseStatus)status object:(id)object {
    switch (status) {
        case WSPurchaseStatusSuccess:
        {
            [WSToast simple:APPLocalized(@"充值成功")];
        }
            break;
        case WSPurchaseStatusCancel:
            [WSToast simple:APPLocalized(@"支付失敗")];
            break;
        default:
            break;
    }
}
#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WSRechargeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSRechargeCell" forIndexPath:indexPath];
    [cell setDataWithObject:self.dataArray[indexPath.row]];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = WSScreenWidth - 12 - 12;
    size.height = 51.5 + 12;
    return size;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        WSPurchaseFooterView * footerView;
        if (!footerView) {
            footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"WSPurchaseFooterView" forIndexPath:indexPath];
            footerView.delegate = self;
        }
        return footerView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    CGSize size = CGSizeZero;
    size.width = WSScreenWidth;
    size.height = [WSPurchaseFooterView cellHeight] + 26;
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self payGoods:self.dataArray[indexPath.row]];
    
}

#pragma mark WSPurchaseFooterViewDelegate
- (void)purchaseFooterViewWebActionWithUrl:(NSString *)url titleStr:(NSString *)titleStr {
    UIViewController * target = [[CTMediator sharedInstance]ct_showWebWithUrl:url titleStr:titleStr showNav:YES closeHandle:^{}];
    [self.navigationController pushViewController:target animated:YES];
}

@end
